package com.alibaba.citrus.cr.forecast.facade.service;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.forecast.facade.converter.QueryForecastRequestConverter;
import com.alibaba.citrus.cr.forecast.repository.ForecastOrderQueryRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crforecastsales.api.forecastorderlinequery.service.ForecastOrderLineQueryService;
import com.epoch.app.crforecastsales.model.dto.PageQueryForecastRequest;
import com.epoch.app.crforecastsales.model.dto.SearchForecastOrderRequest;
import com.epoch.app.crforecastsales.otsforecastorderquery.service.OtsForecastOrderQueryService;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.List;

import javax.annotation.Resource;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/11/14 15:57
 */
@Component
@Primary
public class OtsForecastOrderQueryServiceImpl implements OtsForecastOrderQueryService {
    @Autowired
    ForecastOrderQueryRepository forecastOrderQueryRepository;
    @Resource
    ForecastOrderLineQueryService forecastOrderLineQueryService;
//    @Autowired
//    private EpochCacheService epochCacheService;
    private final Log log = Log.getLogger(OtsForecastOrderQueryServiceImpl.class);
    @Override
    @FacadeInvoker
    public Result<List<ForecastOrderSDO>> queryForecastOrderList(PageQueryForecastRequest request){
        log.info("OtsForecastOrderQueryServiceImpl queryForecastOrderList request:{}", JSON.toJSONString(request));
        SearchForecastOrderRequest searchRequest = QueryForecastRequestConverter.toSearchForecastOrderRequest(request);
//        epochCacheService.put();

        return forecastOrderQueryRepository.pageQueryByEs(searchRequest);
    }

    @Override
    @FacadeInvoker
    public Result<Long> countForecastOrder(PageQueryForecastRequest request){
        SearchForecastOrderRequest searchRequest = QueryForecastRequestConverter.toSearchForecastOrderRequest(request);
        log.info("OtsForecastOrderQueryServiceImpl_countForecastOrder_request:{}", JSON.toJSONString(searchRequest));
        return forecastOrderQueryRepository.countForecastOrder(searchRequest);
    }

    @Override
    public  Result<String> sumMoneyForecastOrder(PageQueryForecastRequest pageQueryForecastRequest) {
        SearchForecastOrderRequest searchRequest = QueryForecastRequestConverter.toSearchForecastOrderRequest(pageQueryForecastRequest);
        log.info("OtsForecastOrderQueryServiceImpl_countForecastOrdermoney_request:{}", JSON.toJSONString(searchRequest));

        return forecastOrderLineQueryService.sumMoneyOrderLine(searchRequest);
    }
}
